<template>
  <div>
    <div>
      <div>
        <span style="font-size: 10px; margin-left: 20px; margin-top: 20px">版本号2021-03-16</span>
<!--        <el-button type="warning" plain size="mini" @click="dialogVisible = true" style="float: right">密码重置</el-button>-->
        <el-button type="primary" plain size="mini" @click.native="logout" style="float: right; margin-top: 10px; margin-right: 20px; margin-bottom: -50px">登出账号</el-button>
      </div>
      <div>
      <el-dialog
        title="重置密码" :visible.sync="dialogVisible" width="500px">
        <el-form :model="ruleForm" label-position="right"
                 status-icon :rules="rules" ref="ruleForm" label-width="100px">
          <el-form-item label="登录密码:" prop="pass" required>
            <el-input class="form-input" type="password" v-model="ruleForm.pass" autocomplete="off" auto-complete="new-password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码:" prop="checkPass" required>
            <el-input class="form-input" type="password" v-model="ruleForm.checkPass" autocomplete="off" auto-complete="new-password"></el-input>
          </el-form-item>
          <el-form-item style="float:right;">
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
        <span>升职加薪、迎娶白富美</span>
      </el-dialog>
      </div>
    </div>

<!--    <el-collapse-transition name="el-fade-in" style="float: left">-->
      <el-tabs v-model="activeName" @tab-click="handleClick" tabPosition="top" style="margin: 20px 20px -10px; float: left">
        <el-tab-pane :label="item.groupName" v-for="item in groupList" :name="item.groupCode" :key="item.groupCode">
          <tableList style="float: left" :ref="'table' + item.groupId"/>
          <uploadList style="float: right; margin-left: 30px" :group-id="groupId" :ref="'upload' + item.groupId"/>
        </el-tab-pane>
      </el-tabs>
<!--    </el-collapse-transition>-->


  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { reset } from '@/api/user'
import { getGroup } from '@/api/group'
import { getWorkStatist } from '@/api/statist'
import tableList from './table'
import uploadList from './uploadList'
export default {
  components: {
    tableList,
    uploadList
  },
  computed: {
      ...mapGetters([
        'sidebar',
        'avatar'
      ])
  },
  mounted: function init() {
    this.initData();
  },
  methods: {
    handleClick() {
      getWorkStatist(this.activeName).then(response=>{
        this.groupId = parseInt(this.activeName)
        this.$refs['table' + this.activeName][0].getTableData(response)
        this.$refs['upload' + this.activeName][0].init(this.groupId)
      })
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            reset(this.ruleForm).then(response => {
              if (response.code===400) {
                return
              }
              this.dialogVisible = false
              }).catch(error => {
                console.log(error)
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    initData() {
      getGroup().then(response => {
        this.groupList = response
        this.groupList.forEach(e => {
          e.groupCode = ''+e.groupId
        })
        if (response.length > 0) {
          this.activeName = response[0].groupCode
          this.groupId = response[0].groupId
          this.handleClick(response[0])
        }
      })
    }
  },
  data() {
    let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
    let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      dialogVisible: false,
      activeName: '',
      groupId: '',
      groupList: [],
      ruleForm: {
        pass: '',
        checkPass: '',
        userId: '',
        group: []
      },
      rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
